/**
@ 作者: 秦东
@ 时间: 2024-12-04 11:35:11
@ 功能: 登录人员信息
*/
import { defineStore } from 'pinia';
import { useStorage } from "@vueuse/core";
import { stores } from "@/utils/pinia/index";

import { loginStruct } from '@/api/login/type'
import { loginApi } from '@/api/login/index'

const appTokenKey = import.meta.env.VITE_APP_TOKEN_KEY  //系统关键之

export const userStror = defineStore("user",() => {
    const appToken = useStorage(appTokenKey, "");
    const userKey = useStorage("userKey", "");
    const userToken = useStorage("userToken", "");
    const roles = ref<string[]>([]); // 用户角色编码集合 → 判断路由权限
    const perms = ref<string[]>([]); // 用户权限编码集合 → 判断按钮权限
    const userInfo = ref<any>("");

    /**
    @ 作者: 秦东
    @ 时间: 2024-12-04 11:38:20
    @ 功能: 登录
    */
    const login = (val:loginStruct) => {
        return new Promise<void>((resolve, reject) => {
            loginApi(val)
            .then((data:any) => {
                resolve();
                return data
            })
            .catch((error:any) => {
                reject(error);
            });
        });
    }
    /**
    @ 作者: 秦东
    @ 时间: 2024-10-23 11:19:17
    @ 功能: 清空值
    */
    const setUserInfoNull = () => {
        appToken.value = "";
        userKey.value = "";
        userToken.value = "";
        roles.value = [];
        perms.value = [];
        userInfo.value = "";
    }
    return {
        appToken,
        userKey,
        userToken,
        roles,
        perms,
        userInfo,
        login,
        setUserInfoNull
    }
})


export function useUserStoreHook() {
    return userStror(stores);
}